#include "stdafx.h"
#include "Jet.h"

#include "AfterBurnerClone.h"
#include "../Framework/ContentManager.h"
#include "../Framework/Toolkit.h"

namespace MyGame {

Jet::Jet(Game* game) : Flying(game) {
    this->cannonDelay = 0.05f;
    this->cannonSound = NULL;
    this->cannonTimer = 0;
    this->explosionSound = NULL;
    this->flightSound = NULL;
    this->health = 0;
    this->missileSpeed = 20;
    this->missileTurnSpeed = 1;
    this->targetYawPitchRoll = D3DXVECTOR3(0, 0, 0);
    this->yawPitchRollSpeed = 2;
    this->SetModelDirection(D3DXVECTOR3(0, 0, 1));
    this->SetMeshFile("F-18.x");
    this->SetEffectFile("PhongShader.fx");
}

Jet::~Jet(void) {
    d3d::Delete(this->cannonSound);
    d3d::Delete(this->explosionSound);
    d3d::Delete(this->flightSound);
}

int Jet::GetHealth() const {
    return this->health;
}

void Jet::SetHealth(int val) {
    this->health = val;
    if (this->health <= 0) {
        if (this->GetState() == FLYINGSTATE_ALIVE) {
            this->SetState(FLYINGSTATE_DYING);
            this->particleEmitter->SetPosition(this->GetPosition());
            for (int i = 0; i < 50; ++i) {
                D3DXVECTOR3 speed = D3DXVECTOR3(
                                        (FLOAT)Toolkit::RandomFloat(-3, 3),
                                        (FLOAT)Toolkit::RandomFloat(0, 6),
                                        (FLOAT)Toolkit::RandomFloat(-3, 3));
                this->particleEmitter->SetParticleLife((FLOAT)Toolkit::RandomFloat(0.5, 1));
                this->particleEmitter->SetSpeed(speed);
                this->particleEmitter->Emit();
            }

            if (this->explosionSound != NULL) {
                D3DXVECTOR3 pos = this->GetPosition();
                this->explosionSound->GetBuffer3D()->SetPosition(pos.x, pos.y, pos.z, DS3D_DEFERRED);
                this->explosionSound->GetBuffer()->SetCurrentPosition(0);
                this->explosionSound->GetBuffer()->Play(0, 0, 0);
            }

            this->SetEnabled(false);
        }

        this->health = 0;
    }
}

void Jet::InflictDamage(int hp) {
    this->SetHealth(this->health - hp);
}

void Jet::ShootMissile() {
}

void Jet::ShootCannon() {
    if (this->cannonTimer <= 0) {
        Bullet* bullet = new Bullet(this->GetGame());
        bullet->SetPosition(this->GetPosition());
        ((AfterBurnerClone*)this->GetGame())->GetEnemyController()->GetBullets()->push_back(bullet);
        this->cannonTimer = this->cannonDelay;
        if (this->cannonSound != NULL) {
            D3DXVECTOR3 pos = this->GetPosition();
            this->cannonSound->GetBuffer3D()->SetPosition(pos.x, pos.y, pos.z, DS3D_DEFERRED);
            this->cannonSound->GetBuffer()->SetCurrentPosition(0);
            this->cannonSound->GetBuffer()->Play(0, 0, 0);
        }
    }
}

void Jet::Draw(FLOAT timeDelta) {
    D3DXVECTOR3 vLightDir = D3DXVECTOR3(1, 1, 1);
    D3DXVec3Normalize(&vLightDir, &vLightDir);
    D3DXCOLOR vLightDiffuse = D3DXCOLOR(1, 1, 1, 1);
    this->effect->SetValue("g_LightDir", &vLightDir, sizeof(D3DXVECTOR3));
    this->effect->SetValue("g_LightDiffuse", &vLightDiffuse, sizeof(D3DXCOLOR)) ;

    D3DXMATRIX mWorld = this->GetWorldMatrix();
    D3DXMATRIX mWorldViewProjection = mWorld * this->GetGame()->GetCamera()->GetViewMatrix() * this->GetGame()->GetCamera()->GetProjectionMatrix();
    this->effect->SetMatrix("g_mWorldViewProjection", &mWorldViewProjection) ;
    this->effect->SetMatrix("g_mWorld", &mWorld) ;

    D3DXCOLOR colorMtrlAmbient(0.1f, 0.1f, 0.1f, 1.0f);
    D3DXCOLOR colorMtrlDiffuse(1.0f, 1.0f, 1.0f, 1.0f);
    D3DXCOLOR colorMtrlSpecular(1.0f, 1.0f, 1.0f, 1);

    this->effect->SetValue("g_MaterialAmbientColor", &colorMtrlAmbient, sizeof(D3DXCOLOR));
    this->effect->SetValue("g_MaterialDiffuseColor", &colorMtrlDiffuse, sizeof(D3DXCOLOR));
    this->effect->SetValue("g_MaterialSpecularColor", &colorMtrlSpecular, sizeof(D3DXCOLOR));
    this->effect->SetTechnique("Phong_Shader");

    __super::Draw(timeDelta);
}

void Jet::Initialize() {
    __super::Initialize();

    this->particleEmitter = new ParticleEmitter(this->GetGame());
    this->particleEmitter->SetBeginScale(D3DXVECTOR3(2, 2, 2));
    this->particleEmitter->SetEndScale(D3DXVECTOR3(0.5, 0.5, 0.5));
    this->particleEmitter->SetParticleSystem(((AfterBurnerClone*)this->GetGame())->GetParticleSystem());
    this->particleEmitter->SetGravity(D3DXVECTOR3(0, -10, 0));
    this->particleEmitter->SetParticleLife(2);
    this->particleEmitter->SetPeriod(0.02f);
    this->particleEmitter->SetEffect(this->particleEmitter->GetParticleSystem()->GetExplosion());
}

void Jet::LoadContent() {
    __super::LoadContent();

    this->cannonSound = this->GetGame()->GetContentManager()->LoadSound("MachineGun.wav");
    this->explosionSound = this->GetGame()->GetContentManager()->LoadSound("Explosion.wav");
    this->flightSound = this->GetGame()->GetContentManager()->LoadSound("Jet.wav");
    if (this->flightSound != NULL) {
        D3DXVECTOR3 pos = this->GetPosition();
        this->flightSound->GetBuffer3D()->SetPosition(pos.x, pos.y, pos.z, DS3D_DEFERRED);
        this->flightSound->GetBuffer()->SetCurrentPosition(0);
        this->flightSound->GetBuffer()->SetVolume(DSBVOLUME_MAX / 4);
        this->flightSound->GetBuffer()->Play(0, 0, DSBPLAY_LOOPING);
    }
}

void Jet::Update(FLOAT timeDelta) {
    D3DXVECTOR3 v = this->targetYawPitchRoll - this->GetRotation();
    FLOAT distance = D3DXVec3Length(&v);
    if (distance != 0) {
        FLOAT x = this->yawPitchRollSpeed * timeDelta;

        if (distance <= x) {
            this->GetRotation() = this->targetYawPitchRoll;
        } else {
            D3DXVec3Normalize(&v, &v);
            this->SetRotation(this->GetRotation() + v * x);
        }

        D3DXMatrixRotationYawPitchRoll(&this->rotationMatrix, this->GetRotation().y, this->GetRotation().x, this->GetRotation().z);
    }

    if (this->cannonTimer > 0) {
        this->cannonTimer -= timeDelta;
    }

    __super::Update(timeDelta);

    if (this->flightSound != NULL) {
        D3DXVECTOR3 pos = this->GetPosition();
        this->flightSound->GetBuffer3D()->SetPosition(pos.x, pos.y, pos.z, DS3D_DEFERRED);
    }
}

void Jet::YawPitchRollTo(D3DXVECTOR3 yawPitchRoll) {
    this->targetYawPitchRoll = yawPitchRoll;
}

}
